package com.wdh.kjs.fragment;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.app.Fragment;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.wdh.kjs.Adapter.InvestmentListAdapter;
import com.wdh.kjs.Adapter.ListViewForScrollView;
import com.wdh.kjs.R;
import com.wdh.kjs.activity.InvestmentDetailActivity;
import com.wdh.kjs.http.Httpurl;
import com.wdh.kjs.model.JsonModel;
import com.wdh.kjs.model.PlatformInfoForAppHtml;
import com.wdh.kjs.util.ToastUtils;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by codingWw on 2016/3/7.
 */
public class InvestmentDetailOneFragment extends Fragment {
    private WebView frage_investdetail_text;
    private JsonModel jsonModel;
    private String level;
    private TextView frage_investdetail_notice;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_investmentdetail_one, container, false);
        frage_investdetail_text = (WebView) view.findViewById(R.id.frage_investdetail_text);
        SharedPreferences sharedPreferences = getActivity().getSharedPreferences("logininfo", Context.MODE_PRIVATE);
        level = sharedPreferences.getString("levels", "");
//        if (level)
        intweb();
        return view;
    }

    private void intweb() {
        RequestParams params = new RequestParams();

        params.addBodyParameter("itemId", InvestmentDetailActivity.itemId);
        params.addBodyParameter("page", 1 + "");
        params.addBodyParameter("rows", "1");
        HttpUtils http = new HttpUtils();
        http.send(HttpRequest.HttpMethod.POST, Httpurl.URL_Invest_DETAIL, params, new RequestCallBack<String>() {

            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {


                Gson gson = new Gson();
                jsonModel = gson.fromJson(responseInfo.result, JsonModel.class);

                if (jsonModel.success) {

                    item();
                }


            }

            @Override
            public void onFailure(HttpException error, String msg) {

                ToastUtils.showShort(getActivity(), "网络异常！");
            }
        });

    }

    private void item() {
//        if (jsonModel.getItemOtherInfo().size()!= 0) {
//            if (jsonModel.getItemOtherInfo().get(0).getItemInfoType() == 0)
//                frage_investdetail_text.setText(Html.fromHtml(jsonModel.getItemOtherInfo().get(0).getItemOtherContext()));
//            else if(jsonModel.getItemOtherInfo().get(1).getItemInfoType() == 0)
//                frage_investdetail_text.setText(Html.fromHtml(jsonModel.getItemOtherInfo().get(1).getItemOtherContext()));
//        }
        for (int i=0;i<jsonModel.getItemOtherInfo().size();i++){
            if (jsonModel.getItemOtherInfo().get(i).getItemInfoType() == 1){
//                frage_investdetail_text.setText(Html.fromHtml(jsonModel.getItemOtherInfo().get(i).getItemOtherContext()));
                frage_investdetail_text.loadDataWithBaseURL(null,jsonModel.getItemOtherInfo().get(i).getItemOtherContext(), "text/html", "utf-8", null);
                frage_investdetail_text.getSettings().setJavaScriptEnabled(true);
                frage_investdetail_text.setWebChromeClient(new WebChromeClient());
            }
        }
    }

}
